package com.hanbang.maige.view.login.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TextInputLayout;
import android.view.View;
import android.widget.TextView;

import com.hanbang.maige.R;
import com.hanbang.maige.code.base.view.activity.BaseMvpActivity;
import com.hanbang.maige.mode.javabean.base.UserData;
import com.hanbang.maige.mode.javabean.login.BiaoQianData;
import com.hanbang.maige.pressenter.othre.LoginPresenter;
import com.hanbang.maige.utils.other.CountdownUtils;
import com.hanbang.maige.utils.other.EditHelper;
import com.hanbang.maige.utils.other.Validators;
import com.hanbang.maige.utils.ui.SnackbarUtil;
import com.hanbang.maige.view.home.activity.HomeActivity;
import com.hanbang.maige.view.login.ILogInBaseView;
import com.hanbang.maige.widget.button.FlatButton;
import com.hanbang.maige.widget.dialog.DialogZhuChe;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * 登录页面
 * *
 */
public class LoginActivity extends BaseMvpActivity<ILogInBaseView.IloginView, LoginPresenter> implements ILogInBaseView.IloginView {
    public EditHelper editHelper = new EditHelper(this);
    boolean isSkipHome;
    @BindView(R.id.phone)
    TextInputLayout phone;
    @BindView(R.id.password)
    TextInputLayout password;
    @BindView(R.id.user_login_row)
    FlatButton userLoginRow;
    @BindView(R.id.zhuche)
    FlatButton zhuche;
    DialogZhuChe zhuCheDialog;


    String zhucheCode;
    String logInCode;
    @BindView(R.id.fasong)
    TextView fasong;

    @Override
    public int getContentView() {
        return R.layout.activity_login;
    }

    @Override
    public int getStatusBarPaddingTop() {
        return 0;
    }

    @Override
    public int getStatusBarResource() {
        return R.color.translucent;
    }

    /**
     * 跳转到主页
     *
     * @param context
     */
    public static void startUI(Context context) {
        startUI(context, true);
    }

    /**
     * 返回到当前页
     *
     * @param context
     * @param isSkipHome
     */
    public static void startUI(Context context, boolean isSkipHome) {
        Intent intent = new Intent(context, LoginActivity.class);
        intent.putExtra("isSkipHome", isSkipHome);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();
    }

    @Override
    public void parseIntent(Intent intent) {
        isSkipHome = intent.getBooleanExtra("isSkipHome", true);
    }

    /*
     * 初始化view
     */
    public void initView() {
        editHelper.addEditHelperData(
                new EditHelper.EditHelperData(phone, Validators.getLengthSRegex(1, 11), "请正确输入11位手机号"));
        editHelper.addEditHelperData(new EditHelper.EditHelperData(password, Validators.getLengthSRegex(1, 4), "验证码为4位"));
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    @Override
    public LoginPresenter initPressenter() {
        return new LoginPresenter();
    }


    @Override
    public void clearData() {

    }


    @Override
    public void logInCode(String code) {
        logInCode = code;
        CountdownUtils.recordTime(fasong);
    }

    @Override
    public void zhuCheCode(String code) {
        zhucheCode = code;
        zhuCheDialog.setCode();
    }

    @Override
    public void login(UserData UserData, String msg) {

        showSnackbar(msg, SnackbarUtil.Info, true, new Snackbar.Callback() {
            @Override
            public void onDismissed(Snackbar snackbar, int event) {
                super.onDismissed(snackbar, event);
                HomeActivity.startUI(LoginActivity.this);
                finish();
            }
        });
    }

    @Override
    public void zhuche(UserData UserData, String msg) {
        showSnackbar(msg, SnackbarUtil.Info, true, new Snackbar.Callback() {
            @Override
            public void onDismissed(Snackbar snackbar, int event) {
                super.onDismissed(snackbar, event);
                HomeActivity.startUI(LoginActivity.this);
                finish();
            }
        });

    }


    @OnClick(R.id.user_login_row)
    public void onClick(View view) {
        if (editHelper.check()) {
            presenter.login(
                    editHelper.getText(R.id.phone),
                    editHelper.getText(R.id.password));
        }

    }

    @OnClick(R.id.zhuche)
    public void onClickZhuChe() {
        if (zhuCheDialog == null) {
            zhuCheDialog = new DialogZhuChe(this);
        }
        zhuCheDialog.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK && zhuCheDialog != null) {
            zhuCheDialog.setBiaoQian((BiaoQianData.ChildLeiBean) data.getParcelableExtra("data"));
        }
    }

    @OnClick(R.id.fasong)
    public void onClickFasong(View view) {
        if (editHelper.getEditHelperData(R.id.phone).check(this)) {
            presenter.getLogInCode(editHelper.getText(R.id.phone));
        }
    }
}
